/**
 * 
 */
package com.ims.stocks.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.ims.common.action.BaseAction;
import com.ims.common.utils.CodeValueData;
import com.ims.common.utils.propertyManager;
import com.ims.stocks.model.Product;
import com.ims.stocks.service.product.ProductService;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @author Achal B.I
 *
 */
public class ProductAction extends BaseAction implements ServletRequestAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8489842795720983328L;
	/**
	 * 
	 */
	
	private List<CodeValueData> productsHeaderList = new ArrayList<CodeValueData>();
	private List<Product> productsList = new ArrayList<Product>();
	
	private ProductService productService;
	
	private final String PRODUCT_HEADER_LIST = "common.product.header";
	private String crudType =""; 
	private Product product = new Product();
	HttpServletRequest request;
	

	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		
	}
	public HttpServletRequest getServletRequest() {
		return this.request;
	}
	
	public String execute(){
		
		loadAction();
		return SUCCESS;
		
	}
	
	private String getProductVal() throws Exception{
		
		 return SUCCESS;
	}
	
		private void loadAction() {
			String Id = getServletRequest().getParameter("id");
			setCrudType(getServletRequest().getParameter("pType"));
			if(Id!=null&&!Id.isEmpty()){
				product = (Product) productService.findById(Product.class, Integer.parseInt(Id) );
			}
			if(crudType!=null&&crudType.equalsIgnoreCase("Delete")){
				productService.txDelete(product);
				addActionMessage("Record deleted successfully");
			}
			productsList = productService.findAll(Product.class);
			productsHeaderList = propertyManager.getInstance().getListFromProp(PRODUCT_HEADER_LIST);
		}

		@SuppressWarnings("unchecked")
		public String addProduct() throws Exception{
			 
			//save it
			
			productService.txSaveOrUpdateObject(product);
	 
			
	 
			return SUCCESS;
	 
		}
	/**
	 * @param productService the productService to set
	 */
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	/**
	 * @return the productService
	 */
	public ProductService getProductService() {
		return productService;
	}

	/**
	 * @return the productsList
	 */
	public List<Product> getProductsList() {
		return productsList;
	}

	/**
	 * @param productsList the productsList to set
	 */
	public void setProductsList(List<Product> productsList) {
		this.productsList = productsList;
	}

	/**
	 * @return the productsHeaderList
	 */
	public List<CodeValueData> getProductsHeaderList() {
		return productsHeaderList;
	}

	/**
	 * @param productsHeaderList the productsHeaderList to set
	 */
	public void setProductsHeaderList(List<CodeValueData> productsHeaderList) {
		this.productsHeaderList = productsHeaderList;
	}


	/**
	 * @return the product
	 */
	public Product getProduct() {
		return product;
	}
	/**
	 * @param product the product to set
	 */
	public void setProduct(Product product) {
		this.product = product;
	}
	/**
	 * @return the crudType
	 */
	public String getCrudType() {
		return crudType;
	}
	/**
	 * @param crudType the crudType to set
	 */
	public void setCrudType(String crudType) {
		this.crudType = crudType;
	}










}
